//
//  Feedback.m
//  K-Music
//
//  Created by CHUN on 16/1/15.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "Feedback.h"

@interface Feedback ()<UITextViewDelegate,UIAlertViewDelegate>
{
    UITextView *Feed;
}


@end

@implementation Feedback

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)loadView
{
    [super loadView];

    self.automaticallyAdjustsScrollViewInsets = NO;
    Feed = [[UITextView alloc]initWithFrame:CGRectMake(20, 20, self.view.frame.size.width-40, 150)];
    Feed.delegate = self;
    Feed.layer.borderWidth = 1.5;
    Feed.tag = 543245;
    Feed.layer.borderColor = RGB(208, 208, 208).CGColor;
    Feed.font = [UIFont fontWithName:@"Arial" size:15];
    Feed.textColor = [UIColor lightGrayColor];
    Feed.text = @"请在这里输入...";
    [self.view addSubview:Feed];

    
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH/32, 250 ,WIDTH-WIDTH/16, WIDTH/10)];
    
    [button addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    button.clipsToBounds = YES;
    button.backgroundColor = RGB(240, 176, 39);
    
    [self.view addSubview:button];

}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = RGB(241, 241, 241);
    self.navigationItem.title = @"我要吐槽";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
    [self tapView];
}

- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)submit
{
    if (Feed.text.length ==0 ) {
        [MBProgressHUD showErrorWithStatus:@"输入不能为空！" toView:self.view];
    }
    else
    {
        
        AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
        
        NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
        
        manager.requestSerializer =[AFJSONRequestSerializer serializer];
        
        [manager.requestSerializer setValue:@"application/json"
                         forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                         forHTTPHeaderField:@"Authorization"];
        
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];

        NSString*infoStr11=[NSString stringWithFormat:@"/api/feedback"];
        
        NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
        
        NSDictionary * submit = @{@"content":Feed.text};
        
        [manager POST:intactURL parameters:submit success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             NSDictionary * result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
             
             NSLog(@"================:%@",result);
             NSDictionary * body = [result objectForKey:@"body"];
             
             NSString * abc = [NSString stringWithFormat:@"以收录 ID- %@ - 意见 - %@ -",[body objectForKey:@"createdBy"],[body objectForKey:@"content"]];
             
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:abc delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
             alert.tag = 10086;
             [alert show];
             
         }failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             NSLog(@"%@", error);
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"提交失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
             [alert show];
         }];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 10086) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}



#pragma mark-UItextView代理方法-
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if ([Feed.text isEqualToString:@"请在这里输入..."]) {
        textView.text=@"";
        Feed.textColor = [UIColor blackColor];
    }
    
    return YES;
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    if ([Feed.text isEqualToString:@""]) {
        Feed.textColor = [UIColor lightGrayColor];
        Feed.text = @"请在这里输入...";
    }
    return YES;
}

//收键盘手势
- (void)tapView
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(CancelkeyBoard)];
    [self.view addGestureRecognizer:tap];
}

- (void)CancelkeyBoard
{
    [ Feed resignFirstResponder];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
